/*
 Exercise 6.21
 Display analogue sensor readings 
 with 4 x 7-segment module
 and two  74HC595 shift registers
 http://tronixstuff.com/tutorials > Chapter Six
 */

int latchpin = 9; // connect to pin 12 on the '595
int clockpin = 7; // connect to pin 11 on the '595
int datapin = 10; // connect to pin 14 on the '595
int sensor = 0; // value from analogue sensor. 
int lhd = 0;
int mhd = 0;
int mhd2 = 0;
int rhd = 0;
float a = 0;
int b = 0;
int c= 0;
float d = 0;
int leadingzero = 0; // 0 for no leading zereos, 1 for leading zeroes
int rnum = 0;


int segdisp[10] = {
  238,72,214,220,120,188,190,200,254,252}; // base 10 equivalents for digits 0~9
int posdisp[4] = {
  1,2,4,8}; // base 10 equivalents to close cathodes on display 0~3 on module

void setup()
{
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
  randomSeed(analogRead(0));

}


void cleardisplay()
// turns off all segments of all digits 
{
  for (int aa=0; aa<4; aa++)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, posdisp[aa]); // sets the digit to address
    shiftOut(datapin, clockpin, MSBFIRST, 0); // clears the digit
    digitalWrite(latchpin, HIGH);
  }
}

void digitdisplay(int digit, int location)
// displays "digit" on display "location" 0~3
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, posdisp[location]); // sets the digit to address
  shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit]); // clears the digit
  digitalWrite(latchpin, HIGH);
}

void digitpointdisplay(int digit, int location)
// displays "digit" on display "location" 0~3 with the decimal point 
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, posdisp[location]); // sets the digit to address
  shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit]+1); // clears the digit
  digitalWrite(latchpin, HIGH);
}

void onedigitnumber(int subject)
// displays a one-digit number on the display module with leading zeroes
{
  cleardisplay();
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
    digitdisplay(0,2);
  }
  digitdisplay(subject,3);
}

void twodigitnumber(int subject)
// displays a two-digit number on the display module with leading zeroes
{
  cleardisplay();
  rhd = subject % 10;
  a = subject/10;
  lhd = int(a);
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
  }
  digitdisplay(lhd,2);
  digitdisplay(rhd,3);
}

void threedigitnumber(int subject)
// displays a three-digit number on the display module with leading zeroes
{
  cleardisplay();
  a = subject/100;
  lhd = int(a);
  a = subject/10;
  b = int(a); 
  mhd = b % 10;
  b=subject%100;
  rhd=b%10;
  if (leadingzero==1)
  {
    digitdisplay(0,0);
  }
  digitdisplay(lhd,1);
  digitdisplay(mhd,2);
  digitdisplay(rhd,3);
}

void fourdigitnumber(int subject)
// displays a four-digit number on the display module with leading zeros
{
  cleardisplay();
  a = subject/1000;
  lhd = int(a);
  b=lhd*1000;
  c=subject-b;
  a = c/100;
  mhd = int(a);
  a = c/10;
  b = int(a); 
  mhd2 = b % 10;
  b=subject%1000;
  c=b%100;
  rhd=c%10;
  digitdisplay(lhd,0);
  digitdisplay(mhd,1);
  digitdisplay(mhd2,2);
  digitdisplay(rhd,3);
}

void displaynumber(int rawnumber)
// takes an integer and displays it on our 4-digit LED display module
{
  if (rawnumber>=0 && rawnumber<10)
  {
    onedigitnumber(rawnumber);
  } 
  else if (rawnumber>=10 && rawnumber<100)
  {
    twodigitnumber(rawnumber);
  } 
  else if (rawnumber>=100 && rawnumber<1000)
  {
    threedigitnumber(rawnumber);
  } 
  else if (rawnumber>=1000)
  {
    fourdigitnumber(rawnumber);
  }
}

void loop()
{

  for (int zzz=0; zzz<10000; zzz++)
  {
    displaynumber(zzz);
    delay(100);
  }
  
}




















